<?php
namespace App\Controllers\Sys;

use App\Controllers\BaseController;
use App\Models\SysMenu;

class MenuController extends BaseController {

    public function initialize() {
        parent::initialize();
    }

    //----------------------------------------------------------------
    // 菜单信息列表
    //----------------------------------------------------------------
    public function indexAction() {
        $this->display('sys/menu/index');
    }

    //----------------------------------------------------------------
    // 获取菜单信息
    //----------------------------------------------------------------
    public function getsAction() {
        $menu = new SysMenu();
        $ret  = $menu->getMenus();
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        }
        return $this->sendAjax(false, "获取数据失败，系统错误！");
    }

    //----------------------------------------------------------------
    // 新增菜单信息
    //----------------------------------------------------------------
    public function addAction() {
        if($this->request->isGet()) {
            $this->display('/sys/menu/add');
        } else {
            $data = [
                'name'       => trim($this->request->getPost('name')),
                'load_event' => 'menuLoad',
                'url'        => trim($this->request->getPost('url')),
                'status'     => $this->request->getPost('status'),
                'is_nav'     => $this->request->getPost('is_nav'),
                'up_id'      => $this->request->getPost('up_id'),
                'icon'       => trim($this->request->getPost('icon')),
                'priority'   => intval($this->request->getPost('priority')),
            ];
            if($data['up_id'] == '') {
                $data['up_id'] = 0;
            }
            if($data['name'] == '') {
                return $this->sendAjax(false, '新增菜单信息失败，菜单名称不能为空！');
            }
            if($data['url'] == '') {
                return $this->sendAjax(false, '新增菜单信息失败，菜单地址不能为空！');
            }
            if($data['status'] == '' || !is_numeric($data['status'])) {
                return $this->sendAjax(false, '新增菜单信息失败，菜单的状态值必须选择！');
            }
            if($data['is_nav'] == '' || !is_numeric($data['is_nav'])) {
                return $this->sendAjax(false, '新增菜单信息失败，菜单在导航栏是否显示必须选择！');
            }
            if($data['priority'] == '' || !is_numeric($data['priority'])) {
                return $this->sendAjax(false, '新增菜单信息失败，菜单权重值不能为空！');
            }

            $menu = new SysMenu();
            $data = $menu->add($data);
            if($data) {
                return $this->sendAjax(true, '新增菜单信息成功！');
            } else {
                return $this->sendAjax(false, '新增菜单信息失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 修改菜单信息
    //----------------------------------------------------------------
    public function editAction() {
        if($this->request->isGet()) {
            $this->display('/sys/menu/edit');
        } else {
            $data = [
                'id'       => $this->request->getPost('id'),
                'name'     => trim($this->request->getPost('name')),
                'url'      => trim($this->request->getPost('url')),
                'status'   => $this->request->getPost('status'),
                'is_nav'   => $this->request->getPost('is_nav'),
                'up_id'    => $this->request->getPost('up_id'),
                'icon'     => trim($this->request->getPost('icon')),
                'priority' => intval($this->request->getPost('priority')),
            ];
            if($data['name'] == '') {
                return $this->sendAjax(false, '编辑菜单信息失败，菜单名称不能为空！');
            }
            if($data['url'] == '') {
                return $this->sendAjax(false, '编辑菜单信息失败，菜单地址不能为空！');
            }
            if($data['status'] == '' || !is_numeric($data['status'])) {
                return $this->sendAjax(false, '编辑菜单信息失败，菜单的状态值必须选择！');
            }
            if($data['is_nav'] == '' || !is_numeric($data['is_nav'])) {
                return $this->sendAjax(false, '编辑菜单信息失败，菜单在导航栏是否显示必须选择！');
            }
            if($data['up_id'] == '' || !is_numeric($data['up_id'])) {
                return $this->sendAjax(false, '编辑菜单信息失败，菜单上级模块不能为空！');
            }
            if($data['priority'] == '' || !is_numeric($data['priority'])) {
                return $this->sendAjax(false, '编辑菜单信息失败，菜单权重值不能为空！');
            }
            $menu = new SysMenu();
            if($menu->edit($data) === false) {
                return $this->sendAjax(false, "编辑菜单信息失败，系统错误！");
            }
            return $this->sendAjax(true, "编辑菜单信息成功！");
        }
    }

    //----------------------------------------------------------------
    // 根据菜单id来获取菜单信息
    //----------------------------------------------------------------
    public function getAction() {
        $id     = $this->request->getPost('id') != '' ? $this->request->getPost('id') : $this->request->getQuery('id');
        $menu   = new SysMenu();
        $entity = $menu->getMenu($id);
        if($entity !== false) {
            return $this->sendAjax(true, $entity);
        }
        return $this->sendAjax(false, '获取菜单信息失败，系统错误！');
    }

    //----------------------------------------------------------------
    // 获取所有的顶级菜单信息列表
    //----------------------------------------------------------------
    public function getUpMenuAction() {
        $menu   = new SysMenu();
        $entity = $menu->getTops();
        if($entity !== false) {
            return $this->sendAjax(true, $entity);
        }
        return $this->sendAjax(false, '获取菜单信息失败，系统错误！');
    }
}
